#include "timer.h"
#include "ticker.h"

static Timer g_timer[TIMER_MAX];


void Timer_Init(void)
{
	int i = 0;
	
	for(i = 0; i < TIMER_MAX; i++)
	{
		g_timer[i].last = Ticker_Get();
		g_timer[i].timeout = 100;
		g_timer[i].func = 0;
	}
}
void Check_Timer(void)
{
	int i = 0;
	
	for(i = 0; i < TIMER_MAX; i++)
	{
		if( g_timer[i].func )
		{
			unsigned long diff = Ticker_Get() - g_timer[i].last;
			
			if(  diff > g_timer[i].timeout || diff > 0x80000000 )
			{
				g_timer[i].func();
				g_timer[i].last = Ticker_Get();
			}
		}
	}
}
void Set_Timer(int i, unsigned long timeout, void(*func)(void))
{
	if( i < 0 || i >=  TIMER_MAX )
	{
		return;
	}
	g_timer[i].last = Ticker_Get();
	g_timer[i].timeout = timeout;
	g_timer[i].func = func;
}

